<?php


namespace App\Http\Model\Job;


use App\Enum\AccountNettingEnum;
use Illuminate\Support\Facades\DB;

class PfsHeadWithdrawProvisionModel
{

    /**
     * 刷新数据库同步结果-失败
     *
     * @param $exist
     * @param $desc
     *
     * @return int
     */
    public static function failure($exist, $desc)
    {
        return DB::table('account_netting')->where('id', $exist->id)->update([
            'result_desc' => $desc,
            'result'      => AccountNettingEnum::RESULT_FAILURE,
        ]);
    }


    /**
     * 刷新数据库同步结果-成功
     *
     * @param $data
     * @param $result
     * @param $exist
     *
     * @return int
     */
    public static function success($data, $result, $exist)
    {
        return DB::table('account_netting')->where('id', $exist->id)->update([
            'result_desc'         => empty($result->desc) ? '' : $result->desc,
            'result'              => AccountNettingEnum::RESULT_SUCCESS,
            'pfs_transfer_status' => empty($result->transferStatus) ? '-' : $result->transferStatus,
        ]);
    }


    /**
     * 查询账号主体账号信息
     *
     * @param $id
     *
     * @return array|false|mixed
     */
    public static function pfs($id)
    {
        $data = DB::select('SELECT * FROM `pfs_dw_ini` WHERE `id`=:id AND `is_del`=0', [
            'id' => $id,
        ]);
        return empty($data) ? [] : current($data);
    }


    /**
     * 更新记录为未推送
     *
     * @param $exist
     *
     * @return int
     */
    public static function close($exist)
    {
        return DB::table('account_netting')->where('id', $exist->id)->update([
            'result' => AccountNettingEnum::RESULT_WAIT,
        ]);
    }


    /**
     * 更新记录状态为推送中
     *
     * @param $data
     * @param $exist
     *
     * @return bool|int
     */
    public static function push($data, $exist, $pfs)
    {
        if (empty($exist)) {
            /*记录不存在,就写入*/
            $id = DB::table('account_netting')->insertGetId([
                'result'              => AccountNettingEnum::RESULT_ING,
                'push_time'           => date('Y-m-d H:i:s'),
                'pfs_dw_id'           => $data['pfs_dw_id'],
                'transfer_id'         => $data['transfer_id'],
                'create_date'         => $data['create_date'],
                'account_netting'     => $data['account_netting'],
                'apply_reason'        => $data['apply_reason'],
                'apply_type'          => $data['apply_type'],
                'ledger_subject_name' => $pfs->ledger_subject_name,
                'ledger_subject_id'   => $pfs->ledger_subject_id,
                'city_name'           => $pfs->city_name,
                'area_code'           => $pfs->area_code,
            ]);
            return (object) ['id' => $id];
        }
        /*更新*/
        DB::table('account_netting')->where('id', $exist->id)->update([
            'result'       => AccountNettingEnum::RESULT_ING,
            'push_time'    => date('Y-m-d H:i:s'),
            'apply_reason' => $data['apply_reason'],
            'apply_type'   => $data['apply_type'],
        ]);
        return $exist;
    }


    /**
     * 查询记录是否存在
     *
     * @param $data
     *
     * @return array|false|mixed
     */
    public static function exist($data)
    {
        $exist = DB::select('SELECT * FROM `account_netting` WHERE `pfs_dw_id`=:pfs_dw_id AND `create_date`=:create_date AND `is_del`=0', [
            'pfs_dw_id'   => $data['pfs_dw_id'],
            'create_date' => $data['create_date'],
        ]);
        return empty($exist) ? [] : current($exist);
    }

}





/**
 * PfsHeadWithdrawProvisionModel.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/9 10:23:00   操作:创建
 **/
